﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Text;

namespace TSOL.Helpers
{
    public class HTMLCreator
    {
        public class DivTag {
            public static string Write(string ID, string Divclass, Func<string> bodyWrite)
            {
                StringBuilder tagContent = new StringBuilder();
                using (new HTMLCreator.TagLife(() => { tagContent.Append(CreateDivStart(ID, Divclass)); return true; },
                                                    () => { tagContent.Append(CreateDivEnd()); return true; }
                                              ))
                {
                    tagContent.Append(bodyWrite());
                }

                return tagContent.ToString();
            }


        }

        public class SpanTag
        {
            public static string Write(string Class, Func<string> bodyWrite)
            {

                StringBuilder tagContent = new StringBuilder();
                using (new HTMLCreator.TagLife(() => { tagContent.Append(CreateSpanStart(Class)); return true; },
                                                    () => { tagContent.Append(CreateSpanEnd()); return true; }
                                              ))
                {
                    tagContent.Append(bodyWrite());
                }

                return tagContent.ToString();
            }


        }
        public class TableTag
        {
            public static string Write(string ID, string Class, string BorderWidth, string CellPadding, string CellSpacing, Func<string> bodyWrite)
            {
                StringBuilder tagContent = new StringBuilder();
                using (new HTMLCreator.TagLife(() => { tagContent.Append(Table.Create(ID, Class, BorderWidth, CellPadding, CellSpacing)); return true; },
                                                    () => { tagContent.Append(Table.End()); return true; }
                                              ))
                {
                    tagContent.Append(bodyWrite());
                }

                return tagContent.ToString();
            }


        }

        public class TableColumnTag
        {
            public static string Write(Func<string> bodyWrite, [Optional, DefaultParameterValue("0")] string Colspan, [Optional, DefaultParameterValue("auto")] string Width, [Optional, DefaultParameterValue("left")] string Align, [Optional, DefaultParameterValue("baseline")] string Valign, [Optional, DefaultParameterValue("")] string Class)
            {
                StringBuilder tagContent = new StringBuilder();
                using (new HTMLCreator.TagLife(() => { tagContent.Append(Table.CreateColumn(Colspan, Width, Align, Valign, Class)); return true; },
                                                    () => { tagContent.Append(Table.EndColumn()); return true; }
                                              ))
                {
                    tagContent.Append(bodyWrite());
                }

                return tagContent.ToString();
            }
        }

        public class TableRowTag
        {
            public static string Write(Func<string> bodyWrite, [Optional, DefaultParameterValue("left")] string Align, [Optional, DefaultParameterValue("baseline")] string Valign, [Optional, DefaultParameterValue("")] string Class)
            {
                StringBuilder tagContent = new StringBuilder();
                using (new HTMLCreator.TagLife(() => { tagContent.Append(Table.CreateRow(Align, Valign, Class)); return true; },
                                                    () => { tagContent.Append(Table.EndRow()); return true; }
                                              ))
                {
                    tagContent.Append(bodyWrite());
                }

                return tagContent.ToString();
            }
        }


        public class SelectTag
        {
            public static string Write(string Id, string Name, string OnChange, Func<string> bodyWrite)
            {
                StringBuilder tagContent = new StringBuilder();
                using (new HTMLCreator.TagLife(() => { tagContent.Append(CreateSelectStart(Id, Name, OnChange)); return true; },
                                                    () => { tagContent.Append(CreateSelectEnd()); return true; }
                                              ))
                {
                    tagContent.Append(bodyWrite());
                }

                return tagContent.ToString();
            }

            public static string Write(string Id, string Name, Func<string> bodyWrite)
            {
                return Write(Id, Name, "", bodyWrite);    
            }
        }

        
        
        public class TagLife : IDisposable
        {

            event Func<bool> oEndTagFunc;

            public TagLife(Func<bool> startFunc, Func<bool> endFunc)
            {
                oEndTagFunc = endFunc;
                startFunc();
            }

            public void Dispose()
            {
                oEndTagFunc(); 
            }
        }


        public class Table
        {

            public static string Create(string ID, string Class, string BorderWidth, string CellPadding, string CellSpacing)
            {
                return String.Format("<table id=\"{0}\" class=\"{1}\" border=\"{2}\" cellpadding=\"{3}\" cellspacing=\"{4}\" />", ID, Class, BorderWidth, CellPadding, CellSpacing);
            }
            
            public static string CreateRow([Optional, DefaultParameterValue("left")] string Align, [Optional, DefaultParameterValue("baseline")] string Valign, [Optional, DefaultParameterValue("")] string Class)
            {
                return String.Format("<tr align=\"{0}\" valign=\"{1}\" class=\"{2}\" />", Align, Valign, Class);
            }

            public static string CreateColumn([Optional, DefaultParameterValue("0")] string Colspan, [Optional, DefaultParameterValue("auto")] string Width, [Optional, DefaultParameterValue("left")] string Align, [Optional, DefaultParameterValue("baseline")] string Valign, [Optional, DefaultParameterValue("")] string Class)
            {
                return String.Format("<td colspan=\"{0}\" width=\"{1}\" align=\"{2}\" valign=\"{3}\" class=\"{4}\" />", Colspan, Width, Align, Valign, Class);
            }

            public static string EndColumn()
            {
                return String.Format("</td>");
            }

            public static string EndRow()
            {
                return String.Format("</tr>");
            }

            public static string End()
            {
                return String.Format("</table>");
            }

        }

        public static string CreateAnchor(string Href, string Title, string Class, string Content)
        {
            if (Class != null && Class.Trim() != "")
                Class = String.Format(" class=\"{0}\" ", Class);
            else
                Class = "";
                
            return String.Format("<a href=\"{0}\" {1} title=\"{2}\" >{3}</a>", Href, Class, Title, Content);
        }

        public static string CreateAnchor(string Href, string target, string onClick, string onMouseover, string Content)
        {
            return String.Format("<a href=\"{0}\" target=\"{1}\"  onClick=\"{2}\" onMouseover=\"{3}\" >{4}</a>", Href, target, onClick, onMouseover, Content);
        }

        public static string CreateImage(string Href, string AlternativeText, string Class, [Optional, DefaultParameterValue("")] string Width, [Optional, DefaultParameterValue("")] string Height)
        {
            string width = "", height = "";

            #region Optionals
                if (Width != "") width = "width=\"" + Width + "\"";
                if (Height != "") height = "height=\"" + Height + "\"";
            #endregion 

            return String.Format("<img src=\"{0}\" alt=\"{1}\" class=\"{2}\" {3} {4} />", Href, AlternativeText, Class, width, height);
        }


        public static string CreateSpanStart(string Class)
        {
            return String.Format("<span class=\"{0}\" >", Class);
        }

        public static string CreateSpanEnd()
        {
            return String.Format("</span>");
        }

        public static string CreateDivStart(string Class)
        {
            return String.Format("<div class=\"{0}\" >", Class);
        }

        public static string CreateDivStart(string ID, string Class)
        {
            return String.Format("<div id=\"{0}\" class=\"{1}\" >", ID, Class);
        }

        public static string CreateDivEnd()
        {
            return "</div>";
        }

        public static string CreateULStart(string Class)
        {
            return String.Format("<ul class=\"{0}\" >", Class);
        }

        public static string CreateULEnd()
        {
            return "</ul>";
        }

        public static string CreateLineItem(string Content, string Class)
        {
            return string.Format("<li class=\"{0}\">{1}</li>", Class, Content);
        }

        public static string CreateInput(string Type, string Id, string Name, string Value, string selected)
        {
            return String.Format("<input type=\"{0}\" id=\"{1}\" name=\"{2}\" value=\"{3}\" {4} />", Type, Id, Name, Value, selected);
        }

        public static string CreateImageButton(string Id, string Name, string Source, string Style, string OnClick)
        {
            return String.Format("<input type=\"image\" id=\"{0}\" name=\"{1}\" style=\"{2}\" src=\"{3}\" onclick=\"{4}\" />", Id, Name, Style, Source, OnClick );
        }

        public static string CreateCheckbox(string Id, string Name, string Value, bool selected)
        {
            if (selected)
                return CreateInput("checkbox", Id, Name, Value, "checked");
            else
                return CreateInput("checkbox", Id, Name, Value, "");

        }

        public static string CreateSelectStart(string Id, string Name, string OnChange)
        {
            if (OnChange!="")
            {
                OnChange = "OnChange=\""+ OnChange + "\"";
            }
            return String.Format("<select id=\"{0}\" name=\"{1}\" {2}>", Id, Name, OnChange);
        }

        public static string CreateOption(string Content, string Value, bool Selected)
        {
            string SelectedText = "";
            if (Selected)
            {
                SelectedText = "selected=\"selected\"";
            }

            return String.Format("<option {0} value=\"{1}\">{2}<option\">", SelectedText, Value, Content);
        }


        public static string CreateSelectEnd()
        {
            return "</select>";
        }

    }
}